package org.ims.inv.service.project.impl;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.ims.inv.dao.project.DAOProjectProcess;
import org.ims.inv.model.flow.confer.ModelFlowConfer;
import org.ims.inv.model.project.ModelProjectProcess;
import org.ims.inv.service.project.ServiceProjectProcess;

import cn.trymore.core.exception.ServiceException;
import cn.trymore.core.service.impl.ServiceGenericImpl;
import cn.trymore.core.util.UtilString;

/**
 * Service: 项目流程
 * 
 * @author Jeccy.Zhao
 *
 */
public class ServiceProjectProcessImpl
extends ServiceGenericImpl<ModelProjectProcess> implements ServiceProjectProcess
{
	private DAOProjectProcess daoProjectProcess;
	
	public ServiceProjectProcessImpl(DAOProjectProcess dao)
	{
		super(dao);
		this.daoProjectProcess = dao;
	}

	public void setDaoProjectProcess(DAOProjectProcess daoProjectProcess)
	{
		this.daoProjectProcess = daoProjectProcess;
	}

	public DAOProjectProcess getDaoProjectProcess()
	{
		return daoProjectProcess;
	}
	
	/*
	 * (non-Javadoc)
	 * @see org.ims.inv.service.project.ServiceProjectProcess#getProcessByNodeKey(java.lang.String)
	 */
	@Override
	public List<ModelProjectProcess> getProcessByNodeKey(String nodeKey)
			throws ServiceException
	{
		if (UtilString.isNotEmpty(nodeKey))
		{
			ModelProjectProcess entity = new ModelProjectProcess();
			entity.setNodeKey(nodeKey);
			
			return this.getByCriterion(entity);
		}
		
		return null;
	}
	
	/*
	 * (non-Javadoc)
	 * @see org.ims.inv.service.project.ServiceProjectProcess#getProcessByDependedFlow(java.lang.String)
	 */
	@Override
	public List<ModelProjectProcess> getProcessByDependedFlow (
			String dependFlowId) throws ServiceException
	{
		if (UtilString.isNotEmpty(dependFlowId))
		{
			ModelProjectProcess entity = new ModelProjectProcess();
			
			ModelFlowConfer confer = new ModelFlowConfer();
			confer.setId(dependFlowId);
			
			entity.setDependedFlow(confer);
			
			return this.getByCriterion(entity);
		}
		
		return null;
	}
	
	/*
	 * (non-Javadoc)
	 * @see cn.trymore.core.service.impl.ServiceGenericImpl#getCriterias(cn.trymore.core.model.ModelBase)
	 */
	@Override
	protected DetachedCriteria getCriterias(ModelProjectProcess domain)
	{
		DetachedCriteria criteria = DetachedCriteria.forClass(ModelProjectProcess.class);
		
		if (domain != null)
		{
			if (UtilString.isNotEmpty(domain.getNodeKey()))
			{
				criteria.add(Restrictions.eq("nodeKey", domain.getNodeKey()));
			}
			
			if (domain.getDependedFlow() != null && 
					UtilString.isNotEmpty(domain.getDependedFlow().getId()))
			{
				criteria.createCriteria("dependedFlow").add(
						Restrictions.eq("id", domain.getDependedFlow().getId()));
			}
			
		}
		
		return criteria;
	}
	

}
